'''
breif:
    Web demo for GLM-4-Flash
    This demo is based on the GLM-4-Flash model and the ZhipuAI API,
    This demo provides model web search ability and stream output.
    For more details: https://www.bigmodel.cn/dev/howuse/websearch
'''
from zhipuai import ZhipuAI
from datetime import datetime
import json

content  =  open('API.json','r',encoding='utf-8').read()
api = json.loads(content)['api']
client = ZhipuAI(api_key=api)
current_date = datetime.now().strftime("%Y-%m-%d")

class AI():
    def __init__(self):
        ...
    def chat(self,text):
        response = client.chat.completions.create(
        model="glm-4-flash",
        messages=[
            {
                "role": "system",
                "content": f"""你是一个具备网络访问能力的智能助手，在适当情况下，优先使用网络信息（参考信息）来回答，
                            以确保用户得到最新、准确的帮助。当前日期是 {current_date}。"""
            },
            {
                "role": "user",
                "content": text
            }
        ],
        top_p= 0.7,
        temperature= 0.95,
        max_tokens=1024,
        tools = [{
            "type": "web_search",
            "web_search": {
                "enable": True  # 启用网络搜索
            }
        }],
        stream=True
        )
        for trunk in response:
            content = content = trunk.choices[0].delta.content
            print(content, end="")
if __name__ == '__main__':
    ai = AI()
    ai.chat('你好,今天重庆天气如何')